define(function(require){
	var $=require("jq.js");
	var Dialog=require("pz_use_dialog.js");
	
	$(".advisory").on("click",function(){
		$(".ask-form-content").show();
	});
	
	$(".J_askId").click(function(){
		var textValue = $(this).parent().parent().prev().find("textarea").val();
		if($.trim(textValue) ==""){
			var tips=new Dialog({
				  type:"alert",
				  text:"提示信息",
				  tipsText:"输入内容不能为空！",
				  buttonSureText:"知道了", 
				  position:[-150,50],
				  mask:0.3,
				  isDrag:true                //开起拖动
				  });
			return;
		};
		$.post("/front/presalesConsult/addPresalesConsult.do",{content:textValue,articleId:$(this).attr("data-productid"),random:Math.random},function(ret){
			if(ret.ret == -2){
				window.location.href = "/login.do?toUrl=/product/"+$(".J_askId").attr("data-productid")+".html";
			}else{
				var tips=new Dialog({
					  type:"confirm",
					  text:"提示",
					  tipsText:"<p><strong style='font-weight:bold;'>"+ret.msg+"</strong></p>",
					  buttonSureText:"查看咨询", 
					  buttonCancelText:"关闭本层", 
					  position:[-150,50],
					  callBack:function(){         //回调函数
						  window.location.href = "/front/presalesConsult/centerConsultList.do";
					  },
					  cancelCallBack:function(){
						  $(".ask-form-content").hide();
					  },
					  mask:"transparent",
					  isDrag:true                //开起拖动
					  });
			};
		},"json");
	});
	
	function J_ProductDetailRemark(remark){
		var _this_=this;
		//保存评论列表
		this.remarkItemBoxs=$("div.item",remark);
		
		this.remarkItemBoxs.parent().on("click",function(e){
			var target=$(e.target);
			//如果是回复按钮
			if(target.attr("data-role")=="replyButton"){
				if(target.attr("isopen")=="no"){
					_this_.loadReplys(5,$(target).attr("data-comid"),target.parent());
				}else{
					_this_.toggleReplyBox(target.parent());
				};
			};
			//如果是有用
			if(target.attr("data-role")=="youYongButton"){
				_this_.youYongButton(target);
			};
			//如果是继续回复按钮
			if(target.attr("data-role")=="replyToButton"){
				_this_.replyToButton(target);
			};
			//如果是toggleNextInputBox
			if(target.attr("data-role")=="toggleNextInputBox"){
				_this_.toggleNextInputBox(target);
			};
			//如果是replyToToButton
			if(target.attr("data-role")=="replyToToButton"){
				_this_.replyToToButton(target);
			};
			//如果是查看更多
			if(target.attr("data-role")=="viewMore"){
				_this_.viewMore(target);
			};
		
			
		});
	};
	J_ProductDetailRemark.prototype={
		youYongButton:function(target){
			$.get("/front/remark/isUseFul.do",{remarkId:target.attr("data-comid"),random:Math.random},function(ret){
				if(ret.ret==1){
					var l=Number($("em",target).text());
						++l;
					$("em",target).text(l);
				}else if(ret.ret == -2){
					window.location.href = "/login.do?toUrl=/product/"+target.attr("data-productid")+".html";
				}else{
					var tips=new PZ_Dialog({
						  type:"tips",
						  text:ret.msg,
						  delay:1
						  });
				};
			},"json")
		},
		viewMore:function(target){
			$.post("/front/reply/replyList.do",{remarkId:target.attr("data-comid"),random:Math.random},function(ret){
					target.parent().prev().html(ret);
					target.parent().remove();
			});
		},
		replyToToButton:function(target){
			this.replyToButton(target);
		},
		toggleNextInputBox:function(target){
			//拿值
			var name=$(target).parent().parent().prev().find("i").text();
			//设置
			$(target).parent().parent().parent().parent().next().find("p span").text(name);
			target.parent().parent().parent().parent().next().toggleClass("item-reply-hide");
		},
		//继续回复
		replyToButton:function(target){
			var $this$=this;
			var val=target.parent().find("input").val();
			var name=target.parent().prev().find("span").text();
			if($.trim(val)==""){
				var tips=new PZ_Dialog({
					  type:"alert",
					  text:"提示信息",
					  tipsText:"输入内容不能为空！",
					  buttonSureText:"知道了", 
					  callBack:function(){         //回调函数
						  target.parent().find("input").focus();
					  },
					  mask:0.3,
					  isDrag:true                //开起拖动
					  });
				return false;
			};
			
			var obj={
					remarkId:target.attr("data-comid"),
					content:val,
					toLoginId:name,
					random:Math.random
			};
			
			$.post("/front/reply/addReply.do",obj,function(result){
				if(result.ret==1){
					$this$.inertReplyItem(result,target);
				}else if(result.ret == -2){
					window.location.href = "/login.do?toUrl=/product/"+target.attr("data-productid")+".html";
				}else{
					var tips=new PZ_Dialog({
						  type:"alert",
						  text:"提示信息",
						  tipsText:"评论发表失败,稍后再发表！",
						  buttonSureText:"知道了", 
						  callBack:function(){         //回调函数
							  target.parent().find("input").focus();
						  },
						  mask:0.3,
						  isDrag:true                //开起拖动
						  });
				};	
			},"json");
		},
		//回复成功插入新一项
		inertReplyItem:function(result,target){
			
			var t='<div class="item-reply reply-default">'+
						'<strong>'+result.replyCount+'</strong>'+
						'<div class="reply-list">'+
						'<div class="reply-con">'+
							'<span class="u-name"><i>'+result.fromLoginId+'</i><em>回复</em>'+result.toLoginId+'&nbsp;:&nbsp;'+
							'</span> '+
							'<span class="u-con">'+result.content+'</span>'+
						'</div>'+
						'<div class="reply-meta">'+
							'<span class="reply-left">'+result.replyTime+'<a data-role="toggleNextInputBox" href="javascript:void(0);">回复</a></span>'+
						'</div>'+
					'</div>'+
				'</div>'+
					'<div class="item-reply reply-lz item-reply-hide item-reply-sub">'+
					'<div class="reply-list no_b">'+
						'<strong></strong>'+
						'<div class="reply-wrap">'+
							'<p>'+
								'<em>回复</em> <span>'+result.fromLoginId+'</span>'+
							'</p>'+
							'<div class="reply-input">'+
								'<div class="fl">'+
									'<input type="text" value="">'+
								'</div>'+
								'<a data-comid='+result.remarkId+' data-role="replyToToButton" href="javascript:void(0);">回复</a>'+
								'<div class="clr"></div>'+
							'</div>'+
							'<div class="ui-tiptext-arrow ui-tiptext-arrowtop">'+
								'<em>◆</em> <span>◆</span>'+
							'</div>'+
						'</div>'+
				
					'</div>'+
				'</div>';
				var l = target.parent().parent().parent().parent().parent().children().length;
				target.parent().parent().parent().parent().parent().children().eq(l).after(t);
				target.parent().parent().parent().parent().parent().find("input").val("");
				if(!target.parent().parent().parent().parent().hasClass("first-reply")){
					target.parent().parent().parent().parent().addClass("item-reply-hide");
				};
				
		},
		//展开关闭回复框
		toggleReplyBox:function(parent){
			parent.next().toggle();
			parent.next().next().toggle();
		},
		//加载回复内容
		loadReplys:function(size,id,parentBox){
			$.post("/front/reply/replyList.do",{limit:size,remarkId:id,random:Math.random},function(ret){
				parentBox.children().eq(0).attr("isopen","yes");
				parentBox.after(ret);
			});
		}	
	};
	J_ProductDetailRemark.init=function(remarks){
		var _this=this;
		remarks.each(function(){
			new _this($(this));
		});
	};
	//return J_ProductDetailRemark;
	J_ProductDetailRemark.init($(".J_ProductDetailRemark"));
});